import { ExceptionFilter, Catch, ArgumentsHost, HttpException } from "@nestjs/common";
import { Request, Response } from "express";

@Catch(HttpException)
export class HttpExceptionFilter implements ExceptionFilter<HttpException> {
    catch(exception: HttpException, host: ArgumentsHost) {
        const ctx = host.switchToHttp();
        const response = ctx.getResponse<Response>();
        const request = ctx.getRequest<Request>();
        const status = exception.getStatus();
        // @todo 记录日志
        console.log("%s %s error: %s", request.method, request.url, exception.message);
        // 发送响应
        response.status(status).json({
            statusCode: status,
            message: exception.message,
            path: request.url,
            ...exception.getResponse,
        });
    }
}
